package com.oma.testcase;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class Decision {
	
	public void decision(WebDriver driver, String dec, String pos){


		((JavascriptExecutor)driver).executeScript("scroll(250,0);");
		((JavascriptExecutor)driver).executeScript("scroll(0,250);");

		
		if(dec == "approve")
		{
			driver.findElement(By.xpath("//a[@id='approveBtn']")).click();
			
			//Remarks
			driver.findElement(By.xpath("//p[contains(., 'Remarks')]"));
			WebElement remarks = driver.findElement(By.xpath("//div[@id='myModal4']/div[2]/form/textarea"));
			remarks.sendKeys("Approved by " + pos + " GC.");
			driver.findElement(By.xpath("//button[contains(., 'Approve')]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

			//Confirmation
			driver.findElement(By.xpath("//button[@id='errorBtn3']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
				
			//Approved
			driver.findElement(By.xpath("//div[@id='modalError']/div[3]/button[1]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			System.out.println("Approved!");
		} 
		
		else if(dec == "disapprove")
		{
			driver.findElement(By.xpath("//a[@id='disapproveBtn']")).click();
			
			//Remarks
			driver.findElement(By.xpath("//p[contains(., 'Remarks')]"));
			WebElement remarks = driver.findElement(By.xpath("//div[@id='myModal']/div[2]/form/textarea"));
			remarks.sendKeys("Disapproved by " + pos + " GC.");
			driver.findElement(By.xpath("//button[contains(., 'Disapprove')]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
				
			//Confirmation
			driver.findElement(By.xpath("//div[@id='modalError3']/div[3]/button[1]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
				
			//Approved
			driver.findElement(By.xpath("//div[@id='modalError']/div[3]/button[1]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			System.out.println("Disapproved!");
		} 
		
		else if(dec == "defer")
		{
			
			driver.findElement(By.xpath("//a[@id='deferBtn']")).click();
			
			//Remarks
			driver.findElement(By.xpath("//p[contains(., 'Remarks')]"));
			WebElement remarks = driver.findElement(By.xpath("//div[@id='myModal5']/div[2]/form/textarea"));
			remarks.sendKeys("Defer by " + pos + " GC.");
			driver.findElement(By.xpath("//button[contains(., 'Defer')]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
				
			//Confirmation
			driver.findElement(By.xpath("//div[@id='modalError3']/div[3]/button[1]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
				
			//Approved
			driver.findElement(By.xpath("//div[@id='modalError']/div[3]/button[1]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			System.out.println("Deferred!");
			
		}
		else {
			System.out.println("Decision options are: \n 1. approve \n 2. disapprove \n 3. defer");
		}
		
		System.out.println(driver.findElement(By.xpath("//div[@class='bar bar-alert']")).getText());
	}
	
}
